package org.example.modules.sys.service.impl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.example.common.exception.RRException;
import org.example.common.utils.JwtUtils;
import org.example.common.utils.R;
import org.example.common.utils.RedisSession;
import org.example.common.utils.RedisUtils;
import org.example.modules.sys.dto.LoginDto;
import org.example.modules.sys.dto.RegisterDto;
import org.example.modules.sys.entity.SysUserEntity;
import org.example.modules.sys.enums.UserStatusEnum;
import org.example.modules.sys.service.SysCaptchaService;
import org.example.modules.sys.service.SysLoginService;
import org.example.modules.sys.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Objects;

@Service
public class SysLoginServiceImpl implements SysLoginService {

    //5分钟后过期
    private final static int EXPIRE = 60 * 5;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysCaptchaService sysCaptchaService;

    @Override
    public String login(LoginDto loginDto) {
        // 验证验证码
        boolean captcha = sysCaptchaService.validate(loginDto.getUuid(), loginDto.getCaptcha());
        if(!captcha) throw new RRException("验证码不正确");

        //用户信息
        SysUserEntity user = sysUserService.queryByUserName(loginDto.getUserName());

        //账号不存在、密码错误
        if(user == null || !user.getPassword().equals(new Sha256Hash(loginDto.getPassword(), user.getSalt()).toHex())) {
            throw new RRException("账号或密码错误");
        }

        //账号锁定
        if(user.getStatus() == UserStatusEnum.IS_LOCK) throw new RRException("账号被锁定，请联系管理员");

        // 验证成功，允许登陆，创建会话并发放会话 token
        String userIdStr = Long.toString(user.getUserId());
        return JwtUtils.createJWT(userIdStr);
    }

    @Override
    @Transactional
    public String register(RegisterDto  registerDto){
        // 验证验证码
        boolean captcha = sysCaptchaService.validate(registerDto.getUuid(), registerDto.getCaptcha());
        if(!captcha) throw new RRException("验证码不正确");

        //用户信息
        SysUserEntity user = sysUserService.queryByMobile(registerDto.getMobile());
        if(!Objects.isNull(user)) throw new RRException("用户已存在，请重新创建");

        // 验证成功，允许登陆，保存用户信息，发放会话 token
        SysUserEntity newUser = new SysUserEntity();
        BeanUtils.copyProperties(registerDto,newUser);
        // 将权限id存入用户权限列表中
        newUser.setRoleIdList(new ArrayList<>());
        newUser.getRoleIdList().add(registerDto.getRoleId());
        sysUserService.saveUser(newUser);
        // 保存完毕后通过用户名获取用户，并以用户的id生成token
        user = sysUserService.queryByUserName(newUser.getUsername());
        String userIdStr = Long.toString(user.getUserId());
        return JwtUtils.createJWT(userIdStr);
    }

    @Override
    public void logout() {
        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
    }
}
